package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.exam.pojo.entity.QuestionFavorite;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;

import java.util.List;

/**
 * 题目收藏Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface QuestionFavoriteMapper extends BaseMapper<QuestionFavorite> {

    /**
     * 根据用户ID查询收藏的题目列表
     * 
     * @param userId 用户ID
     * @return 收藏的题目列表
     */
    @Select("SELECT * FROM exam_question_favorite WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<QuestionFavorite> selectByUserId(@Param("userId") String userId);

    /**
     * 根据题目ID查询收藏该题目的用户列表
     * 
     * @param questionId 题目ID
     * @return 收藏该题目的用户列表
     */
    @Select("SELECT * FROM exam_question_favorite WHERE question_id = #{questionId} ORDER BY create_time DESC")
    List<QuestionFavorite> selectByQuestionId(@Param("questionId") String questionId);

    /**
     * 根据用户ID和题目ID查询收藏记录
     * 
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 收藏记录
     */
    @Select("SELECT * FROM exam_question_favorite WHERE user_id = #{userId} AND question_id = #{questionId}")
    QuestionFavorite selectByUserIdAndQuestionId(@Param("userId") String userId, @Param("questionId") String questionId);

    /**
     * 分页查询用户收藏的题目
     * 
     * @param page 分页参数
     * @param userId 用户ID
     * @return 分页结果
     */
    IPage<QuestionFavorite> selectFavoritePageByUserId(Page<QuestionFavorite> page, @Param("userId") String userId);

    /**
     * 统计用户收藏的题目数量
     * 
     * @param userId 用户ID
     * @return 收藏数量
     */
    @Select("SELECT COUNT(*) FROM exam_question_favorite WHERE user_id = #{userId}")
    Integer countByUserId(@Param("userId") String userId);

    /**
     * 统计题目被收藏的次数
     * 
     * @param questionId 题目ID
     * @return 收藏次数
     */
    @Select("SELECT COUNT(*) FROM exam_question_favorite WHERE question_id = #{questionId}")
    Integer countByQuestionId(@Param("questionId") String questionId);

    /**
     * 检查用户是否收藏了指定题目
     * 
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 是否收藏
     */
    @Select("SELECT COUNT(*) > 0 FROM exam_question_favorite WHERE user_id = #{userId} AND question_id = #{questionId}")
    Boolean isFavorited(@Param("userId") String userId, @Param("questionId") String questionId);

    /**
     * 根据用户ID删除所有收藏
     * 
     * @param userId 用户ID
     * @return 删除行数
     */
    @Delete("DELETE FROM exam_question_favorite WHERE user_id = #{userId}")
    int deleteByUserId(@Param("userId") String userId);

    /**
     * 根据题目ID删除所有收藏
     * 
     * @param questionId 题目ID
     * @return 删除行数
     */
    @Delete("DELETE FROM exam_question_favorite WHERE question_id = #{questionId}")
    int deleteByQuestionId(@Param("questionId") String questionId);

    /**
     * 根据用户ID和题目ID删除收藏
     * 
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 删除行数
     */
    @Delete("DELETE FROM exam_question_favorite WHERE user_id = #{userId} AND question_id = #{questionId}")
    int deleteByUserIdAndQuestionId(@Param("userId") String userId, @Param("questionId") String questionId);

    /**
     * 获取最受欢迎的题目（按收藏次数排序）
     * 
     * @param limit 限制数量
     * @return 最受欢迎的题目ID列表
     */
    @Select("SELECT question_id, COUNT(*) as favorite_count FROM exam_question_favorite " +
            "GROUP BY question_id ORDER BY favorite_count DESC LIMIT #{limit}")
    List<QuestionFavorite> selectMostFavoritedQuestions(@Param("limit") Integer limit);

    /**
     * 获取用户收藏的题目ID列表
     * 
     * @param userId 用户ID
     * @return 题目ID列表
     */
    @Select("SELECT question_id FROM exam_question_favorite WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<String> selectQuestionIdsByUserId(@Param("userId") String userId);
}
